package com.aeye.chatserver.Controller;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.aeye.chatserver.HttpModelUtil;
import com.aeye.chatserver.dto.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/model")
@ResponseBody
public class ModelController {

    @PostMapping("/input")
    public ResultDTO query(@RequestBody ModelDTO modelDTO) {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(0);
        JSONObject jsonObject = JSONObject.parseObject(HttpModelUtil.modelQuery(modelDTO.getInput()));
        resultDTO.setData(jsonObject);
        return resultDTO;

    }

    @PostMapping("/giveUp")
    public ResultDTO giveUp(@RequestBody GiveUpDTO giveUpDTO) {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(0);
        HttpRequest httpRequest = HttpUtil.createPost("http://192.168.16.155/v1/messages/"+ giveUpDTO.getMessageId() + "/feedbacks");
        Map<String, String> params = new HashMap<>();
        params.put("Authorization", "Bearer app-VbnJATTc3bGxrzbzTA4qNyRx");
        params.put("Content-Type", "application/json");
        httpRequest.addHeaders(params);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rating", giveUpDTO.getRating());
        jsonObject.put("user", "abc-123");
        resultDTO.setData(httpRequest.body(jsonObject.toString()).execute().body());
        return resultDTO;

    }

    @PostMapping("/getNextMsg")
    public @ResponseBody ResponseEntity<String> getNextMsg(@RequestBody NextMsgDTO nextMsgDTO) {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(0);
        HttpRequest httpRequest = HttpUtil.createGet("http://192.168.16.155/app/"+ nextMsgDTO.getMessageId() + "/develop#suggested");
        Map<String, String> params = new HashMap<>();
        params.put("Authorization", "Bearer app-VbnJATTc3bGxrzbzTA4qNyRx");
        params.put("Content-Type", "application/json");
        httpRequest.addHeaders(params);
        return ResponseEntity.ok()
                .contentType(MediaType.TEXT_HTML)
                .body(httpRequest.execute().body());

    }

    @PostMapping("/getOldMsg")
    public ResultDTO getOldMsg(@RequestBody OldMsgDTO oldMsgDTO) {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(0);
        HttpRequest httpRequest = HttpUtil.createGet("http://192.168.16.155/v1/messages?user=abc-123&conversation_id="+ oldMsgDTO.getConversationId());
        Map<String, String> params = new HashMap<>();
        params.put("Authorization", "Bearer app-VbnJATTc3bGxrzbzTA4qNyRx");
        params.put("Content-Type", "application/json");
        httpRequest.addHeaders(params);
        JSONObject jsonObject = JSONObject.parseObject(httpRequest.execute().body());
        resultDTO.setData(jsonObject.getJSONArray("data"));
        return resultDTO;

    }
}
